package aex.action;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import aex.domain.Puser;
import aex.service.ArticleCategoryService;
import aex.service.ArticleService;
import aex.service.MedicinesService;
import aex.service.PuserService;
import aex.service.ScoreService;
import aex.util.CU;

import com.opensymphony.xwork2.ModelDriven;

@Controller("PuserAction")
@Scope("prototype")
public class PuserAction extends BaseActon implements ModelDriven<Puser> {

	private static final long serialVersionUID = 1L;


	@Resource(name = ArticleCategoryService.SERVICE_NAME)
	ArticleCategoryService acService;
	
	@Resource(name = MedicinesService.SERVICE_NAME)
	MedicinesService medicinesService;
	
	@Resource(name = PuserService.SERVICE_NAME)
	PuserService puserService;
	
	@Resource(name = ArticleService.SERVICE_NAME)
	ArticleService artService;
	
	@Resource(name = ScoreService.SERVICE_NAME)
	ScoreService scoreService;

	
	
	
	Puser puser = new Puser();

	public PuserAction() {
		CU.deBugPrintln("MedicinesAction 被创建");
	}

	@Override
	public Puser getModel() {
		return puser;
	}

	/**
	 * 处理用户登录请求
	 * 
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public String login() {
		String yzm = request.getParameter("yzm").trim().toLowerCase();
		if (yzm.equals(request.getSession().getAttribute("yzm").toString()
				.toLowerCase())) {
			Puser userLogin = puserService.check(puser);
			if (userLogin != null) {
				
					if(userLogin.getPuState().equals("1")){
						request.getSession().setAttribute("user", userLogin);
						return "index";
					}else{
						request.setAttribute("err", "您的账号已经被禁用！");
						return "login";
					}
				
					
			} else {
				request.setAttribute("err", "您输入的用户名或密码不正确！");
			}
		} else {
			request.setAttribute("err", "验证码错误！");
		}
		return "login";
	}

	/**
	 * 退出系统
	 * @return
	 */
	public String exit(){
		request.getSession().removeAttribute("user");
		return "index";
	}
	
	/**
	 * 用户注册
	 * @return
	 */
	public String register(){
		
		Puser user = new Puser();
		user.setPuAccount(puser.getPuAccount());
		user=puserService.checkExet(user);
		if(user!=null){
			return CU.goMsgPage(request, "该账号已经被占用，请重新填写！", "err", "javascript:history.go(-1)");
		}else{
			puser.setPuPhoto("images/head.jpg");
			puser.setPuAttention(0);
			puser.setPuFans(0);
			puser.setPuState("1");
			puser.setPuScore(0);
			puser.setPuTime(CU.getSqlTime());
			puserService.save(puser);
			return CU.goMsgPage(request, "注册成功请返回首页登陆！", "ok", "#");
		}
	}

	

	
	/**
	 * 更新用户资料
	 * @return
	 */
	public String updateUser(){
		
		Puser newUser=puserService.findById(puser.getPuId());
		newUser.setPuPhoto(request.getParameter("imgPath"));
		newUser.setPuName(puser.getPuName());
		if(puser.getPuSex().equals("1")){
			newUser.setPuSex("男");
		}else if(puser.getPuSex().equals("2")){
			newUser.setPuSex("女");
		}else{
			newUser.setPuSex(null);
		}
		
		newUser.setPuEmail(puser.getPuEmail());
		puserService.update(newUser);
		request.getSession().setAttribute("user",newUser);
		ajaxPrint("资料已经更新！");
		return null;
	}
	
	
	public String updatePassword(){
		Puser newUser=puserService.findById(puser.getPuId());
		newUser.setPuPassword(puser.getPuPassword());
		puserService.update(newUser);
		ajaxPrint("密码已保存！");
		return null;
	}

}
